package com.example.teacherservice.controller;

import com.example.teacherservice.dto.ClassCourseDTO;
import com.example.teacherservice.dto.ClassInfo;
import com.example.teacherservice.dto.StudentClassDTO;
import com.example.teacherservice.dto.StudentClassInfo;
import com.example.teacherservice.exception.ResourceNotFoundException;
import com.example.teacherservice.service.StudentClassService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/student/classes")
public class StudentClassController {

    private final StudentClassService studentClassService;

    @Autowired
    public StudentClassController(StudentClassService studentClassService) {
        this.studentClassService = studentClassService;
    }

    /**
     * 获取学生所有班级信息
     * @param studentId 学生ID
     * @return 班级列表
     */
    @GetMapping("/by-student/{studentId}")
    public ResponseEntity<List<StudentClassInfo>> getClassesByStudentId(@PathVariable Long studentId) {
        List<StudentClassInfo> classes = studentClassService.getClassesByStudentId(studentId);
        return ResponseEntity.ok(classes);
    }

    /**
     * 获取学生班级详细信息
     * @param studentId 学生ID
     * @param classId 班级ID
     * @return 班级详细信息
     */
    @GetMapping("/detail/{studentId}/{classId}")
    public ResponseEntity<StudentClassDTO> getClassDetail(
            @PathVariable Long studentId,
            @PathVariable Long classId) {
        StudentClassDTO classDetail = studentClassService.getClassDetail(studentId, classId);
        return ResponseEntity.ok(classDetail);
    }

    /**
     * 获取班级课程列表
     * @param studentId 学生ID
     * @param classId 班级ID
     * @return 班级课程列表
     */
    @GetMapping("/courses/{studentId}/{classId}")
    public ResponseEntity<List<ClassCourseDTO>> getClassCourses(
            @PathVariable Long studentId,
            @PathVariable Long classId) {
        List<ClassCourseDTO> courses = studentClassService.getClassCourses(studentId, classId);
        return ResponseEntity.ok(courses);
    }

    /**
     * 检查学生是否在指定班级中
     * @param studentId 学生ID
     * @param classId 班级ID
     * @return 检查结果
     */
    @GetMapping("/exists/{studentId}/{classId}")
    public ResponseEntity<Boolean> checkStudentInClass(
            @PathVariable Long studentId,
            @PathVariable Long classId) {
        boolean exists = studentClassService.existsByStudentIdAndClassId(studentId, classId);
        return ResponseEntity.ok(exists);
    }


    /**
     * 添加课程到学生课程表
     * @param studentId 学生ID
     * @param courseId 课程ID
     * @return 操作结果
     */
    @PostMapping("/{studentId}/courses/{courseId}")
    public ResponseEntity<?> addCourseToStudent(
            @PathVariable Long studentId,
            @PathVariable Long courseId) {
        try {
            // 先检查课程是否已存在
            if (studentClassService.isCourseExists(studentId, courseId)) {
                return ResponseEntity.ok("该课程已存在于学生课程表中");
            }

            // 如果不存在则添加
            studentClassService.addCourseToStudent(studentId, courseId);
            return ResponseEntity.ok("课程添加成功");

        } catch (ResourceNotFoundException e) {
            return ResponseEntity.status(HttpStatus.NOT_FOUND)
                    .body(e.getMessage());
        } catch (Exception e) {
            return ResponseEntity.internalServerError()
                    .body("服务器内部错误: " + e.getMessage());
        }
    }


}